#include "gui_connection.h"
//
/*  Save file as gui_connection.cpp  */
/*  incomming class name Gui_Connection */
//
#include <QCloseEvent>

Gui_Connection::Gui_Connection( QWidget* parent )
{
	setupUi( this );
    Load_Connector();
}
//
void Gui_Connection::Load_Connector()
{
    
    fwriteutf8(REQUESTSTREAM,"");  /* make dir if not exist on mirror or reset file to zero  */
    qt_unlink(REQUESTSTREAM);  /* remove file */
    
    
  QString dirsave =  decodeBase64(QString(setter.value("ConnectionList").toString()));
    allname.clear();
    names->clear();
    
            allname = dirsave.split("|");
   for (int i=0;i<allname.size();i++){
           QString actual = allname.at(i);
           if (actual.size() > 2) {
           QListWidgetItem *oggshop = new QListWidgetItem(names);  
           oggshop->setText(actual);
           }
    }
    connect(pushButton_2, SIGNAL(clicked()), this, SLOT(SaveA()));
    connect(names, SIGNAL(itemClicked (QListWidgetItem*)), this , SLOT(ActiveN(QListWidgetItem*)));
    connect(newconnection, SIGNAL(clicked()), this, SLOT(NewC()));
     connect(qmirror, SIGNAL(clicked()), this, SLOT(MirrA()));
    
    
}

void Gui_Connection::ActiveN( QListWidgetItem * niro )
{
    QString nameconnection = niro->text();
    if (nameconnection.size() > 2) {
        ////////listaattiva = true;
        QString namevar =  decodeBase64(QString(setter.value(nameconnection).toString()));
        
            if (namevar.size() > 2) {
            QStringList param;
            param = namevar.split("|");
                if (param.size() > 3) {
                dname->setText(nameconnection);
                durl->setText(param.at(0));
                duser->setText(param.at(1));
                dpass->setText(param.at(2)); 
                reloadafter->setValue(direct_int(param.at(3)));
                }
            }                
        
    }
    
}




void Gui_Connection::SaveA()
{
    QString cname = dname->text();
    int seconnnf = reloadafter->value();
    
    if (cname.size() > 0 && durl->text().size() > 0) {
    
    QString itemvars = encodeBase64(QString("%1|%2|%3|%4")
    .arg(durl->text())
    .arg(duser->text())
    .arg(dpass->text())
    .arg(seconnnf));
    setter.setValue(cname,itemvars);
    if (!allname.contains(cname,Qt::CaseSensitive) ) {
      allname.append(cname);  
    }
    QString listsave = encodeBase64(allname.join("|"));
    setter.setValue("ConnectionList",listsave);
    allname.clear();
    Load_Connector(); 
    }
}

void Gui_Connection::NewC()
{

    QString dirorfile =  QString(setter.value("MirrorDir").toString());
    if (dirorfile.size() < 1) {
        MirrA();
    }
    
     bool ok;
     QString userorg = QInputDialog::getText(this, tr("New Connection"),tr("Connection Name:"), QLineEdit::Normal,"", &ok);
    if (ok and userorg.size() > 1) {
        
            userorg = userorg.replace(" ","_");
    
            dname->setText(QString("<%1>").arg(userorg));
            durl->setText("");
            duser->setText("");
            dpass->setText(""); 
        
    }
}



void Gui_Connection::MirrA()
{
    QString dirsave = QFileDialog::getExistingDirectory(0,tr("Select a Local Path Mirror to Webdav"),QString(setter.value("MirrorDir").toString()),QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    if (dirsave.size() > 3) {
      setter.setValue("MirrorDir",dirsave);
    }
}














